import * as req from '@/request';
import * as c from '@/services/consts';

export async function treeSelect(params: { [key: string]: any }) {
  return req.get<API.VpnGroupTreeSelect>('/vpngroup/treeselect', params);
}

export async function list(
  params: { [key: string]: any },
  sorter?: { [key: string]: any },
  filter?: { [key: string]: any },
) {
  return req.postForm<API.VpnGroupList>(
    '/vpngroup/list',
    params,
    sorter,
    filter,
  );
}

export async function get(data: { key: string }) {
  return req.post<API.VpnGroupViewModel>('/vpngroup/get', data);
}

export async function add(data: { [key: string]: any }) {
  return req.postJson<API.Result>('/vpngroup/create', data);
}

export async function update(data: { [key: string]: any }) {
  return req.postJson<API.Result>('/vpngroup/update', data);
}

export async function remove(data: { key: string }) {
  return req.post<API.Result>('/vpngroup/delete', data);
}

export async function loadGroupTrees(params: { [key: string]: any }) {
  const result = await treeSelect(params);

  //console.log('loadGroupTrees:', result);

  if (result.success && result.data) {
    return Promise.resolve(result.data);
  }
  return Promise.resolve([]);
}

export async function changeProducts(data: {
  keys: string[];
  productIds: number[];
}) {
  return req.postForm<API.Result>('/vpngroup/changeProducts', data);
}

export function getGroupTagTitle(
  group: API.VpnGroupRefModel,
  displayProduct: boolean = false,
  displayMemberLevel: boolean = true,
) {
  let title = '';
  if (displayProduct && group.productName && group.productName != '') {
    title += group.productName + '-';
  }
  if (group.parentName && group.parentName != '') {
    title += group.parentName + '-';
  }
  title += group.name;

  if (displayMemberLevel) {
    title += ` [${c.getMemberLevelText(group.memberLevel)}]`;
  }

  if (group.type == c.VpnGroupType.Ghost.value) {
    title += ` [${c.getVpnGroupTypeText(group.type)}]`;
  }

  return title;
}


export function getGroupBaseTagTitle(
  group: API.VpnGroupBase,
  displayMemberLevel: boolean = true,
) {
  let title = '';
  title += group.name;

  if (displayMemberLevel) {
    title += ` [${c.getMemberLevelText(group.memberLevel)}]`;
  }

  if (group.type == c.VpnGroupType.Ghost.value) {
    title += ` [${c.getVpnGroupTypeText(group.type)}]`;
  }

  return title;
}